package com.yunxin.admin.system.api.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunxin.admin.system.application.DictApplicationService;
import com.yunxin.admin.system.application.DictQueryService;
import com.yunxin.admin.system.application.command.*;
import com.yunxin.admin.system.application.dto.*;
import com.yunxin.admin.system.infra.common.constant.SecurityConstants;
import com.yunxin.admin.system.infra.framework.resubmit.Resubmit;
import com.yunxin.admin.system.infra.framework.syslog.AutoLog;
import com.yunxin.common.result.PageResult;
import com.yunxin.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zero°
 * @since 2023-05-22
 */
@Tag(name = "06.字典接口")
@RestController
@RequestMapping("/api/v1/dict")
@RequiredArgsConstructor
public class SysDictController {

    private final DictApplicationService dictService;

    private final DictQueryService dictQueryService;


    @Operation(summary = "字典分页列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/page")
    public PageResult<DictPageDTO> getDictPage(@ParameterObject DictPageCommand command) {
        Page<DictPageDTO> pageResult = dictQueryService.getDictPage(command);
        return PageResult.success(pageResult.getRecords(), pageResult.getTotal());
    }

    @Operation(summary = "字典数据表单数据", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/{id}/form")
    public Result<DictInfoDTO> getDictForm(@Parameter(description = "字典ID") @PathVariable Long id) {
        DictInfoDTO dictInfoDTO = dictQueryService.getDictInfoById(id);
        return Result.success(dictInfoDTO);
    }

    @Operation(summary = "新增字典", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:dict:add')")
    @Resubmit
    @AutoLog(value = "新增字典")
    public Result<Boolean> saveDict(@RequestBody AddDictCommand command) {
        boolean result = dictService.saveDict(command);
        return Result.judge(result);
    }

    @Operation(summary = "修改字典", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('sys:dict:edit')")
    @AutoLog(value = "修改字典")
    public Result<Boolean> updateDict( @PathVariable Long id, @RequestBody UpdateDeptCommand command) {
        command.setId(id);
        boolean status = dictService.updateDict(command);
        return Result.judge(status);
    }

    @Operation(summary = "删除字典", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:dict:delete')")
    @AutoLog(value = "删除字典")
    public Result<Boolean> deleteDict(@Parameter(description = "字典ID，多个以英文逗号(,)拼接") @PathVariable String ids) {
        boolean result = dictService.deleteDict(ids);
        return Result.judge(result);
    }


    @Operation(summary = "字典下拉列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/options")
    public Result<List<OptionDTO<String>>> listDictOptions(@Parameter(description = "字典类型编码") @RequestParam String typeCode) {
        List<OptionDTO<String>> list = dictQueryService.listDictOptions(typeCode);
        return Result.success(list);
    }


    @Operation(summary = "字典类型分页列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/types/page")
    public PageResult<DictTypePageDTO> getDictTypePage(@ParameterObject DictTypePageCommand command) {
        Page<DictTypePageDTO> pageResult = dictQueryService.getDictTypePage(command);
        return PageResult.success(pageResult.getRecords(), pageResult.getTotal());
    }

    @Operation(summary = "字典类型表单数据", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/types/{id}/form")
    public Result<DictTypeInfoDTO> getDictTypeForm(@Parameter(description = "字典ID") @PathVariable Long id) {
        DictTypeInfoDTO dictTypeInfoDTO = dictQueryService.getDictTypeById(id);
        return Result.success(dictTypeInfoDTO);
    }

    @Operation(summary = "新增字典类型", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/types")
    @PreAuthorize("@ss.hasPerm('sys:dict_type:add')")
    @Resubmit
    @AutoLog(value = "新增字典类型")
    public Result<Boolean> saveDictType(@RequestBody AddDictTypeCommand command) {
        boolean result = dictService.saveDictType(command);
        return Result.judge(result);
    }

    @Operation(summary = "修改字典类型", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PutMapping("/types/{id}")
    @PreAuthorize("@ss.hasPerm('sys:dict_type:edit')")
    @AutoLog(value = "修改字典类型")
    public Result<Boolean> updateDictType(@PathVariable Long id, @RequestBody UpdateDictTypeCommand command) {
        command.setId(id);
        boolean status = dictService.updateDictType(command);
        return Result.judge(status);
    }

    @Operation(summary = "删除字典类型", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @DeleteMapping("/types/{ids}")
    @AutoLog(value = "删除字典类型")
    @PreAuthorize("@ss.hasPerm('sys:dict_type:delete')")
    public Result<Boolean> deleteDictTypes(@Parameter(description = "字典类型ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = dictService.deleteDictTypes(ids);
        return Result.judge(result);
    }

}
